from typing import List


class Solution:
    def maxSumAfterOperation(self, nums: List[int]) -> int:
        ans = 0

        # dp[0] = 未替换的最大子数组; dp[1] = 替换后的最大子数组
        dp = [0, 0]
        for num in nums:
            dp = [max(dp[0] + num, 0),
                  max(dp[0] + num * num, dp[1] + num)]
            ans = max(ans, dp[1])
        return ans


if __name__ == "__main__":
    print(Solution().maxSumAfterOperation(nums=[2, -1, -4, -3]))  # 17
    print(Solution().maxSumAfterOperation(nums=[1, -1, 1, 1, -1, -1, 1]))  # 4
